<?php
class Upload_artwork_model extends MY_Model
{
    function __construct()
    {
        parent::__construct();
       	$this->artworks_table = 'bur_artworks';
        //$this->load->library('session');
    }
    function insert()
    {
        $this->load->database();
		
		
       	$data_artworks=array("artwork_fullname"=>$this->input->post('fullname'),
   					        "artwork_company"=>$this->input->post('company'),	
           					"artwork_telephone"=>$this->input->post('telephone'),	
           					"artwork_email"=>$this->input->post('email'),	
           					"artwork_jobname"=>$this->input->post('jobname'),	
           					"artwork_cenumber"=>$this->input->post('cenumber'),	
           					"artwork_quantity"=>$this->input->post('quantity'),	
           					"artwork_deadline"=>$this->format_date_display($this->input->post('deadline'),$this->input->post('hours'), $this->input->post('minutes') ),
                            "artwork_extranote"=>$this->input->post('extranote')		
                        );
			
		$this->db->insert('bur_artworks',$data_artworks);
		
        return($this->db->insert_id());
    }
    //convert d-m-y to y-m-d
	function format_date_display($date, $hours, $minutes){
    if(empty($date)){
        return;
        }
	//$dateandtime = explode(' ',$date);
    $date_exploded=explode('-',$date);    
    //$date=$date_exploded[2]."-".$date_exploded[1]."-".$date_exploded[0]." ".$dateandtime[1].":00";
	$date=$date_exploded[2]."-".$date_exploded[1]."-".$date_exploded[0]." ".$hours.":".$minutes.":00";
    return $date;
    }
	
    function total_artwork_uploads($region = 0,$search = 'none') 
    {
		$this->db->from($this->artworks_table);
	
		return $this->db->count_all_results();
	}
    
    function get_artworks($limit='false',$offset='false')
    {
    	if ($limit)
        {
        	$limit = (int)$limit;
    		$offset = (int)$offset;
        }
		
		$this->db->order_by('artwork_id','DESC');
      

		if ($limit)
        {
            $query = $this->db->get('bur_artworks',$limit,$offset);
        }
        else
        {
            $query = $this->db->get('bur_artworks');
        }
        
		if($query->num_rows() > 0)
        {
			return $query->result_array();
		}
        else
        {
			return FALSE;
		}
	}
    
   	function get_artworks_detail($artwork_id ='')
    {
		
		$this->db->where('artwork_id',$artwork_id);
	 	$query = $this->db->get('bur_artworks');
		if($query->num_rows() > 0)
        {
			return $query->row_array();
		}
        else
        {
			return FALSE;
		}
	}
    
    function get_artwork_uploads_detail($artwork_id ='')
    {
        $query = $this->db->query("SELECT artwork_filename FROM bur_artworkuploads WHERE artwork_id=".$artwork_id);
		if($query->num_rows() > 0)
        {
            $filename =$query->result_array();
           return $filename;
		}
        else
        {
			return FALSE;
		}
    }

    function delete_artworks($artwork_id )
    {
        if(!empty($artwork_id))
        {
            $this->db->where('artwork_id',$artwork_id);
            $this->db->delete($this->artworks_table);
            
        } 
        else 
        {
            return False;
            
        }
    }
    
    function send_upload_email_admin($insert_id)
    {
        $config = array('mailtype'=>'html');
        
        $uploads=$this->get_artwork_uploads_detail($insert_id);
       
        if(!empty($uploads))
        { 
            $artwork=$uploads;
        }
       
        $data=array("user"=>'admin',
                    "artwork_fullname"=>$this->input->post('fullname'),
                    "artwork_company"=>$this->input->post('company'),	
   					"artwork_telephone"=>$this->input->post('telephone'),	
   					"artwork_email"=>$this->input->post('email'),	
   					"artwork_jobname"=>$this->input->post('jobname'),	
   					"artwork_cenumber"=>$this->input->post('cenumber'),	
   					"artwork_quantity"=>$this->input->post('quantity'),	
   					"artwork_deadline"=>$this->input->post('deadline'),
                    "artwork_extranote"=>$this->input->post('extranote'),
                    "artwork_uploads"=>$artwork
                   );		

        $email_content = $this->load->view(TEMPLATE_NAME.'/front/mail/upload_artwork',$data, true);
      
        $this->load->library('email');
        $this->email->initialize($config);
        $this->email->from($this->input->post('email'));
       
        $this->email->to($this->get_admin_email_list('bur_admin_emails'));
      
        $this->email->subject('New quote request');
        $this->email->message($email_content);                
        $this->email->send();
        return true;	
    }
    
    function send_upload_email_client($insert_id)
    {
        $config = array('mailtype'=>'html');
        
        $uploads=$this->get_artwork_uploads_detail($insert_id);
        if(!empty($uploads))
        {  
            $artwork=$uploads;
        }
     $data=array("user"=>'client',
                    "artwork_fullname"=>$this->input->post('fullname'),
                    "artwork_company"=>$this->input->post('company'),	
   					"artwork_telephone"=>$this->input->post('telephone'),	
   					"artwork_email"=>$this->input->post('email'),	
   					"artwork_jobname"=>$this->input->post('jobname'),	
   					"artwork_cenumber"=>$this->input->post('cenumber'),	
   					"artwork_quantity"=>$this->input->post('quantity'),	
   					"artwork_deadline"=>$this->input->post('deadline'),
                    "artwork_extranote"=>$this->input->post('extranote'),
                    "artwork_uploads"=>$artwork
                   );	
        
        $email_content = $this->load->view(TEMPLATE_NAME.'/front/mail/upload_artwork',$data, true);
        
        $this->load->library('email');
        $this->email->initialize($config);
        $this->email->from('info@mykprisguide.com', 'Mykprisguide');
        
        $this->email->to($this->input->post('email'));     
        $this->email->subject('File Submitted');
        $this->email->message($email_content);                
        $this->email->send();
        return true;	
    }
    
    function get_admin_email_list($table_name)
    {
       $query=$this->db->get($table_name);
       if($query->num_rows()>0)
       {
            $admin_email =$query->result_array();
            foreach($admin_email as $key=>$email):
           
            if(!$email['email_address']=='')
            { $admin_emails[]= $email['email_address'];}
            endforeach;
            return $admin_emails;
        }   
    }
    
    function get_last_inserted_id($table_name) 
    {
    	$this->db->select_max('artwork_id');
    	$query = $this->db->get($table_name);
        if($query->num_rows()>0)
        {
        	$row = $query->row_array();
        	return $row['artwork_id'];
        }
        else
        {
            return 0;
        }
        
    }
    
    function insert_uploads($filename)
    {
        $id=$this->get_last_inserted_id('bur_artworks');
        
       	$data_uploads=array("artwork_id"=>$id+1,
   					        "artwork_filename"=>$filename			
                        );
			
		$this->db->insert('bur_artworkuploads',$data_uploads);
    }
}
?>